package com.tairanchina.md.meeting.resource;

import com.alibaba.fastjson.JSON;
import com.tairanchina.md.constants.CommonConstants;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.constants.MeetingProvider;
import com.tairanchina.md.meeting.dto.ProgramDto;
import com.tairanchina.md.meeting.entity.YearMeetingProgram;
import com.tairanchina.txRestFilter.engine.jersey.TxJerseyTools;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

/**
 * 节目列表
 */
@Path(MeetingConstants.PathRoute.PROGRAM_ROOT)
public class ProgramResource
{
    private Logger logger = LoggerFactory.getLogger(ProgramResource.class);
    @GET
    public Response get(@QueryParam("order") String order)
    {
        if(StringUtils.isBlank(order)){
            order = MeetingConstants.PROGRAM_ORDER;
        }
        String orderColumn = switchOrder(order);
        /**
         * 类实现描述：TODO 获取节目列表, 包括打赏 点赞信息
         * hzyjie 16/12/24 11:44
         */

        ProgramDto programDto = new ProgramDto();
        try {
            programDto =  MeetingProvider.programService.findAllList(orderColumn);

        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取节目列表失败,e:{}",e);
            programDto.setProgramNum(0);
        }
        return TxJerseyTools.returnSuccess(JSON.toJSONString(programDto));
    }

    private String switchOrder(String order) {
        String orderColumn = MeetingConstants.ORDER_MAP.get(order);
        if(StringUtils.isBlank(orderColumn)){
            orderColumn  = MeetingConstants.ORDER_MAP.get(MeetingConstants.PROGRAM_ORDER);
        }
        return orderColumn;
    }

    @GET
    @Path("/id")
    public Response getProgram(@QueryParam("id") String id){
        if(StringUtils.isBlank(id)){
            return TxJerseyTools.returnAbort(CommonConstants.ErrorCode.ERROR_ILLEGAL_PARAMTER);
        }
        YearMeetingProgram program = new YearMeetingProgram();
        try {
            program = MeetingProvider.programService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取节目列表失败,e:{}",e);
        }

        return  TxJerseyTools.returnSuccess(JSON.toJSONString(program));
    }
}
